<?php
/**
 * Created by PhpStorm.
 * User: yappy
 * Date: 2017/7/17
 * Time: 下午8:15
 */


namespace backend\services;
use yii;
use yii\helpers\Url;
use backend\models\User;

class Service
{

    protected function getAllParams()
    {
        return array_merge(Yii::$app->request->post(), Yii::$app->request->get());
    }


    /**
     * @param $key
     * @return null
     */
    protected function getParam($key)
    {
        $allParams = $this->getAllParams();
        return isset($allParams[$key]) ? $allParams[$key] : null;
    }

    public  function getCurrentCompanyId($userId,$roleName)
    {
        $request = Yii::$app->request;
        $companyId =  $request->get('companyId',0);
        if (empty($companyId)) {
            $companyId = Yii::$app->session->get('companyId');
        }
        if(empty($companyId)){
            $getCompany = $this->getCompanyByUser($userId,$roleName);
            if(!empty($getCompany)){
                //如果地址栏未传展会ID 则从数据表中取
                $companyId = $getCompany[0]['id'];
                Yii::$app->session->remove('companyId');
                Yii::$app->session->set('companyId',$companyId);
            }
        }
        return $companyId;
    }


    /**
     * @param $userId
     * @param $roleName
     * @return array|yii\db\ActiveRecord[]
     */
    public  function getCompanyByUser($userId,$roleName)
    {
        if(in_array($roleName,['超级管理员'])|| $userId === 1) {
            $data =  CompanyService::getAllCompany();

        }else {
            $query = User::findOne($userId)->companys;
            $data = array_map(function ($c) {
                return $c->company;
            }, $query);
        }
        return $data;
    }
    # 得到当前的完整url
    public  function getCurrentUrl(){
        return Url::current();
    }

    /**
     * @param $attrUrlStr
     * @param $val
     * @param string $currentUrl
     * @return array|mixed
     */
    public  function getChooseCompanyUrl($attrUrlStr,$val,$currentUrl = '')
    {
        $str = $attrUrlStr.'='.$val;
        $currentRequestVal = Yii::$app->request->get($attrUrlStr);
        $originPUrl = '';

        if($currentRequestVal){
            $originAttrUrlStr = $attrUrlStr.'='.$currentRequestVal;
            if(empty($currentUrl)){
                $currentUrl = $this->getCurrentUrl();
            }
            if($originAttrUrlStr == $str){
//                return str_replace($originAttrUrlStr,$str,$currentUrl);
                $url = $currentUrl;
                if(strstr($currentUrl,'?'.$originAttrUrlStr.'&')){
                    $url = str_replace('?'.$originAttrUrlStr.'&','?',$currentUrl);
                }else if(strstr($currentUrl,'?'.$originAttrUrlStr)){
                    $url = str_replace('?'.$originAttrUrlStr,'',$currentUrl);
                }else if(strstr($currentUrl,'&'.$originAttrUrlStr)){
                    $url = str_replace('&'.$originAttrUrlStr,'',$currentUrl);
                }
                /* if($originPUrl){
                     $url = str_replace($originPUrl,$afterPUrl,$url);
                 }*/
                return [
                    'url'   	=> $url,
                    'selected'	=> true,
                ];
            }else{
                /* if($originPUrl){
                     $currentUrl = str_replace($originPUrl,$afterPUrl,$currentUrl);
                 }*/
                return [
                    'url'   	=> str_replace($originAttrUrlStr,$str,$currentUrl),
                    'selected'	=> false,
                ];
            }
            return str_replace($originAttrUrlStr,$str,$currentUrl);
        }else{
            if(empty($currentUrl)){
                $currentUrl = self::getCurrentUrl();
            }
            if(strstr($currentUrl,'?')){
                /* if($originPUrl){
                     $currentUrl = str_replace($originPUrl,$afterPUrl,$currentUrl);
                 }*/
                return [
                    'url'   	=> $currentUrl.'&'.$str,
                    'selected'	=> false
                ];
            }else{
                /* if($originPUrl){
                     $currentUrl = str_replace($originPUrl,$afterPUrl,$currentUrl);
                 }*/
                return [
                    'url'   	=> $currentUrl.'?'.$str,
                    'selected'	=> false
                ];
            }
        }

    }
    /**
     * @return bool
     */
    public static function getRoleName($userId)
    {
        $roles = Yii::$app->authManager->getAssignments($userId);
        //默认取第一个角色名
        if ($roles) {
            return  array_keys($roles)[0];
        } else {
            return false;
        }

    }
}